package com.yangyang.lamviarm.entity;

import java.util.HashMap;
import java.util.List;

import com.yangyang.lamviarm.util.TypeEnum;

public class JActionDef extends JBaseDef {

	private TypeEnum[] mParamType;
	private String[] mParamName;
	public static HashMap<String, JActionDef> mapNameActionDef;

	public TypeEnum[] getParamType() {
		return mParamType;
	}

	public String[] getParamName() {
		return this.mParamName;
	}

	public JActionDef(List<String> lines) {
		super(lines.get(0));
		this.mParamType = new TypeEnum[lines.size() - 1];
		this.mParamName = new String[lines.size() - 1];
		for (int i = 1; i < lines.size(); i++) {
			this.mParamType[i - 1] = TypeEnum
					.parseType(lines.get(i).split(":")[1]);
			this.mParamName[i - 1] = lines.get(i).split(":")[0];
		}
		if (mapNameActionDef == null)
			mapNameActionDef = new HashMap<String, JActionDef>();
		mapNameActionDef.put(lines.get(0), this);
	}

	// 判断这组谓词能不能符合动作的model定义（变量的数量要一致）
	public boolean fit(List<String> lines) {
		boolean result = true;
		boolean flag[] = new boolean[this.mParamName.length];
		for (int i = 0; i < this.mParamName.length; i++)
			flag[i] = false;
		for (String str : lines) {
			String array[] = str.split("_");
			for (int i = 1; i < array.length; i++) {
				boolean include = false;
				int index = -1;
				for (int j = 0; j < this.mParamName.length; j++) {
					if (this.mParamName[j].equals(array[i])) {
						include = true;
						index = j;
					}
				}
				if (include) {
					flag[index] = true;
				} else {
					result = false;
					break;
				}
			}
			if (!result)
				break;
		}
		for (int i = 0; i < flag.length; i++) {
			if (!result)
				break;
			result &= flag[i];
		}
		return result;
	}

	@Override
	public String toString() {
		String msg = this.mName;
		for (TypeEnum type : this.mParamType)
			msg += type;
		return msg;
	}
}
